package com.zhaolin.service.impl;

import com.zhaolin.entity.Question;
import com.zhaolin.entity.QuestionOpt;
import com.zhaolin.mapper.QuestionDao;
import com.zhaolin.mapper.QuestionOptDao;
import com.zhaolin.service.QuestionService;
import com.zhaolin.utils.BeanMapUtils;
import com.zhaolin.utils.MapControl;
import com.zhaolin.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionDao questionDao;

    @Autowired
    QuestionOptDao questionOptDao;

    @Override
    public int create(Question pi) {
        int flag = 0;
        if(pi.getId() != null){
            flag = this.update(pi);
            questionOptDao.delete(MapParameter.getInstance().add("questionId",pi.getId()).getMap());
        }else{
            flag = questionDao.create(pi);
        }
        if(flag>0){
            List<QuestionOpt> options = pi.getOptions();
            int i = 0;
            for (QuestionOpt option : options) {
                option.setSurveyId(pi.getSurveyId());
                option.setQuestionId(pi.getId());
                option.setOrderby(++i);
                questionOptDao.create(option);
            }
        }
        return pi.getId();

    }

    @Override
    public int delete(int id) {
        int id1 = questionDao.delete(MapControl.getInstance().add("id", id).getMap());
        int questionId = questionOptDao.delete(MapControl.getInstance().add("questionId", id).getMap());
        if (id1>0 && questionId>0){
            return 1;
        }else{
            return 0;
        }

    }

    @Override
    public int update(Question question) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(question)).addId(question.getId()).getMap();
        return questionDao.update(map);
    }

    //查询问卷的问题和答案
    @Override
    public List<Question> query(Question question)
    {
        //先查出问卷的问题
        List<Question> questions = questionDao.query(question);
        //通过问卷的id查出问卷的答案
        Map<String, Object> map = MapControl.getInstance().add("questionId", question.getId()).getMap();
        List<QuestionOpt> questionOpt = questionOptDao.query(map);
        //把所以的问题打包返回
        for ( Question questions1:questions) {
            //新建一个list集合装所以答案
            List<QuestionOpt> list = new ArrayList<>();
            //把所有的答案循环出来装到list集合里
            for (QuestionOpt questionOpt1:questionOpt) {
                //判断答案里的questionId是否和题目id相同
                if (questions1.getId()==questionOpt1.getQuestionId()){
                    //装到集合里
                    list.add(questionOpt1);
                }
                    //再把答案和问题统一打包
                    questions1.setOptions(list);

            }
        }
        return questions;
    }

    @Override
    public Question detail(Map<String, Object> paramMap) {
        return null;
    }

    @Override
    public int count(Question question) {
        return questionDao.count(question);
    }
}
